package pattern

import (
	"fmt"
	"time"

	"jihe.common/protos/base_server/proto"
	"jihe.common/tool"
)

func (s *Model) Create(in *proto.BasePattern) (ret *proto.BaseNull, err error) {
	ret = new(proto.BaseNull)
	in.Id = fmt.Sprintf("%d", tool.GetSnowflakeId())
	in.CreatedAt = time.Now().Format("2006-01-02 15:04:05")
	in.UpdatedAt = time.Now().Format("2006-01-02 15:04:05")
	sqlStr := fmt.Sprintf("INSERT INTO %s (id, name, style, tp, price, remark, created_at, updated_at, url)VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)", s.Name)
	_, err = s.Db.Exec(sqlStr, in.Id, in.Name, in.Style, in.Tp, in.Price, in.Remark, in.CreatedAt, in.UpdatedAt, in.Url)
	if err != nil {
		return
	}
	return
}

func (s *Model) Update(in *proto.BasePattern) (ret *proto.BaseNull, err error) {
	ret = new(proto.BaseNull)
	sqlStr := fmt.Sprintf("UPDATE %s SET name = $1, style = $2, tp = $3, price = $4, remark = $5, updated_at = $6, url = $7 WHERE id = $8", s.Name)
	_, err = s.Db.Exec(sqlStr, in.Name, in.Style, in.Tp, in.Price, in.Remark, time.Now().Format("2006-01-02 15:04:05"), in.Url, in.Id)
	if err != nil {
		return
	}
	return
}

func (s *Model) GetList(in *proto.BaseGetPatternListReq) (ret *proto.BaseGetPatternListRes, err error) {
	ret = new(proto.BaseGetPatternListRes)
	sqlStr := fmt.Sprintf("SELECT id, name, style, tp, price, remark, created_at, updated_at, url FROM %s WHERE 1 = 1", s.Name)
	sqlCountStr := fmt.Sprintf("SELECT COUNT(0) FROM %s WHERE 1 = 1", s.Name)
	conditionSql := ""
	args := make([]interface{}, 0)
	if in.Search != nil {
		if in.Search.Name != "" {
			in.Search.Name = "%" + in.Search.Name + "%"
			args = append(args, in.Search.Name)
			conditionSql += fmt.Sprintf(" AND name ILIKE $%d", len(args))
		}
		if in.Search.Id != "" {
			args = append(args, in.Search.Id)
			conditionSql += fmt.Sprintf(" AND id = $%d", len(args))
		}
		if in.Search.Style != 0 {
			args = append(args, in.Search.Style)
			conditionSql += fmt.Sprintf(" AND style = $%d", len(args))
		}
		if in.Search.Tp != 0 {
			args = append(args, in.Search.Tp)
			conditionSql += fmt.Sprintf(" AND tp = $%d", len(args))
		}
	}
	_ = s.Db.QueryRow(sqlCountStr+conditionSql, args...).Scan(&ret.Total)
	args = append(args, in.PageSize)
	sqlStr = fmt.Sprintf("%s ORDER BY created_at DESC LIMIT $%d", sqlStr+conditionSql, len(args))
	args = append(args, in.Page*in.PageSize)
	sqlStr += fmt.Sprintf(" OFFSET $%d", len(args))
	rows, e := s.Db.Query(sqlStr, args...)
	if e != nil {
		err = e
		return
	}
	defer rows.Close()
	for rows.Next() {
		item := &proto.BasePattern{}
		var (
			createdAt *time.Time
			updatedAt *time.Time
		)
		err = rows.Scan(&item.Id, &item.Name, &item.Style, &item.Tp, &item.Price, &item.Remark, &createdAt, &updatedAt, &item.Url)
		if err != nil {
			return
		}
		if createdAt != nil {
			item.CreatedAt = createdAt.Format("2006-01-02 15:04:05")
		}
		if updatedAt != nil {
			item.UpdatedAt = updatedAt.Format("2006-01-02 15:04:05")
		}
		ret.BasePatternList = append(ret.BasePatternList, item)
	}
	return
}

func (s *Model) GetDetail(id string) (ret *proto.BasePattern, err error) {
	ret = new(proto.BasePattern)
	sqlStr := fmt.Sprintf("SELECT id, name, style, tp, price, remark, created_at, updated_at, url FROM %s WHERE id = $1", s.Name)
	var (
		createdAt *time.Time
		updatedAt *time.Time
	)
	err = s.Db.QueryRow(sqlStr, id).Scan(&ret.Id, &ret.Name, &ret.Style, &ret.Tp, &ret.Price, &ret.Remark, &createdAt, &updatedAt, &ret.Url)
	if err != nil {
		return
	}
	if createdAt != nil {
		ret.CreatedAt = createdAt.Format("2006-01-02 15:04:05")
	}
	if updatedAt != nil {
		ret.UpdatedAt = updatedAt.Format("2006-01-02 15:04:05")
	}
	return
}

func (s *Model) Delete(req *proto.BasePattern) (err error) {
	sqlStr := fmt.Sprintf("DELETE FROM %s where id = $1", s.Name)
	_, err = s.Db.Exec(sqlStr, req.Id)
	if err != nil {
		return
	}
	return
}
